package cn.yzgy.yzgyoa.ucenter.service.impl;

import cn.yzgy.yzgyoa.ucenter.dto.RoleDTO;
import cn.yzgy.yzgyoa.ucenter.entity.SysRole;
import cn.yzgy.yzgyoa.ucenter.entity.SysUser;
import cn.yzgy.yzgyoa.ucenter.mapper.SysRoleMapper;
import cn.yzgy.yzgyoa.ucenter.service.ISysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author Yqsen
 * @since 2019-08-20
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {


    @Override
    public List<SysRole> getRolesByUid(String uid) {
        return baseMapper.getRolesByUid(uid);
    }

    @Override
    public IPage<SysRole> queryAll(IPage<SysRole> ipage, QueryWrapper<SysRole> wrapper) {
        return baseMapper.selectPage(ipage, wrapper);
    }

    @Override
    public int newId() {
        return baseMapper.getNewid();
    }

    @Override
    public int check(SysRole sysRole) {
        int num=1;
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name",sysRole.getRoleName());
        SysRole result=baseMapper.check(wrapper);
        if(result == null)
        {
            num=0;
        }
        return num;
    }

    @Override
    public int addSysRole(SysRole sysRole) {
        return baseMapper.insert(sysRole);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int delSysRole(List<String> roleId) {
        UpdateWrapper<SysRole> wrapper = new UpdateWrapper<>();
        wrapper.in(true,"role_id",roleId);
        return baseMapper.delete(wrapper);
    }

    @Override
    public List<SysRole> getSysRoleList() {
        return baseMapper.getSysRoleList();
    }

    @Override
    public IPage<RoleDTO> queryAllRoleDTO(IPage<RoleDTO> ipage, Integer num, Integer size) {
        ipage.setRecords(baseMapper.queryAll(num,size));
        return ipage;
    }

    @Override
    public String countData() {
        return baseMapper.countData()+"";
    }

    @Override
    public int updateRoleData(Integer result, String way) {
        int num=0;
        String stop="stop";
        String start="start";
        UpdateWrapper<SysUser> wrapper = new UpdateWrapper<>();
        try{
            wrapper.eq("role_id",result);
            if(stop.equals(way)){
                num=baseMapper.stopState(wrapper);
            }
            else if (start.equals(way)){
                num=baseMapper.startState(wrapper);
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return num;
    }

    @Override
    public int changeRoleInfo(SysRole sysRole) {
        UpdateWrapper<SysRole> wrapper = new UpdateWrapper<>();
        wrapper.eq("role_id", sysRole.getRoleId());
        return baseMapper.update(sysRole,wrapper);
    }


}
